﻿Imports System.Data.OleDb

Public Class chat
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Response.ExpiresAbsolute = DateTime.Now.AddDays(-1D)
        Response.Expires = -1500
        Response.CacheControl = "no-cache"


        If Not IsPostBack Then
            populateDropDownUsers()
        End If

        populateGrid()

    End Sub

    Protected Sub populateGrid()

        Dim userID As Int32
        userID = getUserId()

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("dbDusanCS").ConnectionString)
        Dim SqlString As String = "SELECT [UserName], [ToUser], [Message], [DateTimePosted] FROM [ChatQuery] WHERE [UserName] = '" + User.Identity.Name + "'"
        SqlString = SqlString + " OR ToUser = '" + User.Identity.Name + "' ORDER BY [DateTimePosted] DESC"
        SqlDataSource1.SelectCommand = SqlString
        gvChat.DataBind()

    End Sub


    Protected Sub populateDropDownUsers()

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("dbDusanCS").ConnectionString)
        Dim SqlString As String = "Select ID, UserName From Users Where Active = True"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()

        Dim readerUserID As OleDbDataReader = cmd.ExecuteReader()

        dd_Users.DataSource = readerUserID
        dd_Users.DataTextField = "UserName"
        dd_Users.DataValueField = "ID"
        dd_Users.DataBind()


    End Sub

    Protected Sub btn_Chat_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_Chat.Click

        Dim timeNow As String = Date.Now
        timeNow = timeNow.Replace("#", "")

        Dim userID As Int32
        userID = getUserId()

        Dim maxLength As Int32 = chatTextBox.Text.Length

        If maxLength > 30 Then
            maxLength = 30
        End If

        Dim chatText As String = chatTextBox.Text.Substring(0, maxLength)



        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("dbDusanCS").ConnectionString)
        Dim SqlString As String = "Insert into Chat(FromUser,ToUser,Message,DateTimePosted) Values (@param1, @param2,@param3,@param4)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@param1", userID)
        cmd.Parameters.AddWithValue("@param2", dd_Users.SelectedItem.Text)
        cmd.Parameters.AddWithValue("@param3", chatTextBox.Text)
        cmd.Parameters.AddWithValue("@param4", timeNow)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()

        oleDbConn.Close()

        Response.Redirect("ThankYou.aspx?back=chat.aspx")

    End Sub


    Public Function getUserId() As Int32
        Dim userID As Int32

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("dbDusanCS").ConnectionString)
        oleDbConn.Open()

        Dim SqlString As String = "SELECT ID FROM Users WHERE UserName = @param1"
        Dim cmdUserID As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmdUserID.CommandType = CommandType.Text
        cmdUserID.Parameters.AddWithValue("@param1", User.Identity.Name)

        Dim readerUserID As OleDbDataReader = cmdUserID.ExecuteReader()

        If readerUserID.HasRows Then
            Do While readerUserID.Read()
                userID = readerUserID.GetInt32(0)
            Loop
        End If

        Return userID

    End Function

End Class